package com.qk.mangement.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.mangement.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * Date:2025/9/29
 * Author:zxc
 * Description
 * 部门管理的控制层
 */
@Controller
@ResponseBody
@RequestMapping("/depts")
public class DeptController {

    private static final Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有部门");
        return Result.success(deptService.list());
    }

    @DeleteMapping("/{id}")
    public Result addDept(@PathVariable Integer id){
        log.info("删除部门id:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }


    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }






    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("查询部门id:{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }



    @GetMapping
    public Result page(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);
        log.info("分页查询部门,参数:部门名称={},部门状态={},当前页码={},每页大小={}", name, status, page, pageSize);
        log.info("***--------------部门分页查询--------------***");
        return Result.success(pageResult);
    }




    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        try {
            log.info("新增部门:{}",dept);
            deptService.addDept(dept);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success("新增部门失败");
        }



    }



}
